import request from '@/utils/request'
import qs from 'qs';
// 查询学生信息管理列表
export function listStudentManger(query) {
  return request({
    url: '/student/studentManger/list',
    method: 'get',
    params: query
  })
}
export function listAllStudentManger(query) {
  return request({
    url: '/student/studentManger/listVo',
    method: 'get',
    params: query
  })
}
// 查询学生信息管理详细
export function getStudentManger(id) {
  return request({
    url: '/student/studentManger/' + id,
    method: 'get'
  })
}

// 新增学生信息管理
export function addStudentManger(data) {
  return request({
    url: '/student/studentManger',
    method: 'post',
    data: data
  })
}

// 修改学生信息管理
export function updateStudentManger(data) {
  return request({
    url: '/student/studentManger',
    method: 'put',
    data: data
  })
}

// 删除学生信息管理
export function delStudentManger(id) {
  return request({
    url: '/student/studentManger/' + id,
    method: 'delete'
  })
}
//根据专业ID、年级查询学生信息  {student_zy}/{student_nj}
// 前端API文件
export function listByStudentZyAndNj(student_zy, student_nj) {
  return request({
    url: `/student/studentManger/listByStudentZy/${student_zy}/${student_nj}`,
    method: 'get'
  })
}
// 批量分配培养方案
export function assignTrainingToStudents(data) {
  return request({
    url: '/student/studentManger/assignTraining',
    method: 'post',
    data: data
  })
}
export function delAllStudent() {
  return request({
    url: '/student/studentManger/clearAll',
    method: 'delete'
  })
}
/**
 * 根据课程ID、学期ID和年级查询课程排课信息及学生名单
 *
 * @param {string} courseId     课程ID
 * @param {number} semesterId   学期ID
 * @param {number} grade        年级
 * @return {Promise}            返回 Promise 对象
 */
export function getCourseScheduleWithStudents(courseId, semesterId, grade) {
  return request({
    url: '/student/studentManger/getCourseScheduleDetailsAndStudentList',
    method: 'get',
    params: {
      courseId,
      semesterId,
      grade
    }
  });
}

/**
 * 根据培养方案ID列表查询对应的学生名单
 *
 * @param {Array<string>} trainingProgramIds 培养方案ID列表
 * @return {Promise} 返回Promise对象，包含查询到的学生列表
 */
// 彻底重构接口函数，确保配置是有效对象
export function selectStudentInfoListByTrainingProgramIds(trainingProgramIds) {
  // 1. 强制转换为字符串（数组转逗号分隔，单个ID直接用）
  let idsStr = '';
  if (Array.isArray(trainingProgramIds)) {
    idsStr = trainingProgramIds.join(','); // 数组 → "id1,id2"
  } else if (trainingProgramIds) {
    idsStr = String(trainingProgramIds); // 单个ID → "id1"
  }

  // 2. 构造纯净的请求配置对象（仅包含必要字段）
  const requestConfig = {
    url: '/student/studentManger/selectStudentInfoListByTrainingProgramIds',
    method: 'get',
    params: {
      trainingProgramIds: idsStr // 传递字符串，避免数组序列化问题
    }
    // 移除所有可能干扰的配置（如paramsSerializer）
  };

  // 调试：打印配置是否为对象（关键验证）
  console.log("请求配置是否为对象:", typeof requestConfig === 'object' && requestConfig !== null);
  console.log("请求参数:", requestConfig.params);

  return request(requestConfig);
}
/**
 * 根据培养方案ID查询对应的学生列表
 *
 * @param {string} trainingProgramId 培养方案ID
 * @return {Promise} 返回Promise对象，包含查询到的学生列表
 */
export function listStudentsByTrainingProgramId(trainingProgramId) {
  return request({
    url: '/student/studentManger/listByTrainingProgram', // 修改为新的URL
    method: 'get',
    params: { trainingProgramId }
  })
}
// 导入学生导师信息并绑定
export function importStudentTeacher(file) {
  const formData = new FormData();
  formData.append('file', file);
  return request({
    url: '/student/studentManger/importTeacher',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 获取导入进度
export function getImportProgress() {
  return request({
    url: '/student/studentManger/importTeacher/progress',
    method: 'get'
  })
}

// 下载失败记录
export function downloadFailureRecords() {
  return request({
    url: '/student/studentManger/importTeacher/download-failures',
    method: 'get',
    responseType: 'blob'
  })
}


// 根据学生学号查询学生详细信息
export function getStudentInfoByStudentId(studentId) {
  return request({
    url: `/student/studentManger/getStudentInfoByStudentId/${studentId}`,
    method: 'get'
  })
}
export function getStudentInfoByStudentXh(studentId) {
  return request({
    url: `/student/studentManger/getStudentInfoByStudentXh/${studentId}`,
    method: 'get'
  })
}
// 生成导入批次号
export function generateImportBatchNo() {
  return request({
    url: '/student/studentManger/generateImportBatchNo',
    method: 'get'
  })
}

// 导入学生导师关系 - 修改版
export function importStudentTutor(formData) {
  return request({
    url: '/student/studentManger/importTutor',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}


// 查询导入进度
export function queryImportProgress(batchNo) {
  return request({
    url: `/student/studentManger/queryImportProgress/${batchNo}`,
    method: 'get'
  })
}

// 下载失败数据
export function downloadFailureData(batchNo) {
  return request({
    url: `/student/studentManger/downloadFailureData/${batchNo}`,
    method: 'get',
    responseType: 'blob'
  })
}

// 下载导入模板
export function downloadTutorTemplate() {
  return request({
    url: '/student/studentManger/downloadTutorTemplate',
    method: 'POST',
    responseType: 'blob'
  })
}

// 下载导入模板
export function downloadTemplate() {
  return request({
    url: '/student/studentManger/downloadTemplate',
    method: 'get',
    responseType: 'blob'
  })
}
// 根据培养方案ID获取课表
export function getCourseScheduleByTrainingProgramId(trainingProgramId) {
  return request({
    url: `/student/studentManger/getCourseScheduleByTrainingProgramId/${trainingProgramId}`,
    method: 'get'
  })
}

// 通过课程班id获取学生名单，然后查询学生对应的已开课班级以及时间
export function getStudentAndClassByClassId(classId) {
  return request({
    url: `/student/studentManger/getStudentAndClassByClassId/${classId}`,
    method: 'get'
  })
}

// /**根据学生ID获取对应课表*/
// export function getCourseScheduleByStudentId(studentId) {
//   return request({
//     url: `/student/studentManger/getCourseScheduleByStudentId/${studentId}`,
//     method: 'get'
//   })
// }

/**根据学生ID获取对应课表*/
export function getCourseScheduleByStudentId() {
  return request({
    url: `/student/studentManger/getCourseScheduleByStudentId`,
    method: 'get'
  })
}

// 新增：获取学生字段权限配置
export function getStudentFieldPermissions() {
  return request({
    url: '/student/studentManger/fieldPermissions',
    method: 'get'
  })
}